package com.sskj.hhrt.verify;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.hhrt.MVPActivity;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.hhrt.R;
import com.sskj.hhrt.view.SelectPictureDialog;

import java.io.File;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/20
 */
public class Verify2Activity extends MVPActivity<Verify2Presenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.frontImage)
    ImageView frontImage;
    @BindView(R.id.backImage)
    ImageView backImage;


    private SelectPictureDialog selectPictureDialog;

    private final int SELECT_FRONT = 1000;
    private final int SELECT_BACK = 1001;

    private String frontImagePath;
    private String backImagePath;

    private String bankType;
    private String bankNum;
    private String address;

    @Override
    public int getLayoutId() {
        return R.layout.activity_verify2;
    }

    @Override
    public Verify2Presenter getPresenter() {
        return new Verify2Presenter();
    }

    @Override
    public void initView() {
        bankType = getIntent().getStringExtra("bankType");
        bankNum = getIntent().getStringExtra("bankNum");
        address = getIntent().getStringExtra("address");
        ClickUtil.click(frontImage, view -> {
            selectPic(SELECT_FRONT);
        });
        ClickUtil.click(backImage, view -> {
            selectPic(SELECT_BACK);
        });
    }

    @Override
    public void initData() {
        ClickUtil.click(btnNext, view -> {
            if (TextUtils.isEmpty(frontImagePath)) {
                ToastUtils.show("请选择身份证正面照片");
                return;
            }
            if (TextUtils.isEmpty(backImagePath)) {
                ToastUtils.show("请选择身份证反面照片");
                return;
            }
            Verify3Activity.start(this, bankType, bankNum, address,frontImagePath, backImagePath);
//            mPresenter.verify2(account, new File(frontImagePath), new File(backImagePath));
        });
    }

    public static void start(Context context, String bankType, String bankNum, String address) {
        Intent intent = new Intent(context, Verify2Activity.class);
        intent.putExtra("bankType", bankType);
        intent.putExtra("bankNum", bankNum);
        intent.putExtra("address", address);
        context.startActivity(intent);
    }


    public void selectPic(int requestCode) {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(Verify2Activity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(requestCode);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(Verify2Activity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(requestCode);
            }
        });
        selectPictureDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_FRONT || requestCode == SELECT_BACK) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(0);
                    String path;
                    if (media.isCompressed()) {
                        path = media.getCompressPath();
                    } else {
                        path = media.getPath();
                    }
                    if (requestCode == SELECT_FRONT) {
                        frontImagePath = path;
                        frontImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        frontImage.setPadding(0, 0, 0, 0);
                        Glide.with(this).load(path).into(frontImage);
                    } else {
                        backImagePath = path;
                        backImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        backImage.setPadding(0, 0, 0, 0);
                        Glide.with(this).load(path).into(backImage);
                    }
                }
            }
        }
    }

    public void verify2Success() {
//        Verify3Activity.start(this);
//        finish();
    }

}
